package com.sdyd.antivirus.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sdyd.antivirus.dao.GroupInfoMapper;
import com.sdyd.antivirus.dao.OfficeBuildingInfoMapper;
import com.sdyd.antivirus.model.GroupInfo;
import com.sdyd.antivirus.model.OfficeBuildingInfo;
import com.sdyd.antivirus.service.OfficeBuildingInfoService;
import com.sdyd.antivirus.utils.AesEncryptUtil;
import com.sdyd.antivirus.utils.CurrentConditionArray;
import com.sdyd.antivirus.utils.DateUtil;
import com.sdyd.antivirus.utils.FileUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liujia
 * @date 2020/2/10 10:28
 **/
@Service
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
public class OfficeBuildingInfoServiceImpl implements OfficeBuildingInfoService {
    org.slf4j.Logger logger= LoggerFactory.getLogger(OfficeBuildingInfoServiceImpl.class);
    @Autowired
    private OfficeBuildingInfoMapper officeBuildingInfoMapper;
    @Autowired
    private GroupInfoMapper groupInfoMapper;
    @Override
    public JSONObject getUserData(String groupId, String startDate, String endDate) {
        StringBuilder sd = new StringBuilder(startDate);
        startDate=sd.append(" 00:00:00").toString();
        StringBuilder ed = new StringBuilder(DateUtil.getNextDate(endDate));
        endDate = ed.append(" 00:00:00").toString();
        GroupInfo groupInfo = groupInfoMapper.selectByPrimaryKey(groupId);
        String usageScenarios = groupInfo.getResultAddressQr();
        Map<String,String> map = new HashMap<>();
        map.put("groupId",groupId);
        map.put("startdate", startDate);
        map.put("enddate", endDate);
        List<OfficeBuildingInfo> dataByTime = officeBuildingInfoMapper.getDataByTime(map);
        int size = dataByTime.size();
        int[] currentBodyConditionCount = new int[6];
        int[] contactWhPeople = new int[3];
        int[] contactBadConditionPeople = new int[2];
        int[] goToWh = new int[3];
        int[] goOut = new int[2];
        int[] vehicle = new int[8];
        JSONObject jsonObject =new JSONObject();
        for (int i = 0; i <size ; i++) {
            //近期身体状况
            String currentBodyCondition = dataByTime.get(i).getCurrentBodyCondition();
            CurrentConditionArray.currentBodyConditon(currentBodyCondition.split(","),currentBodyConditionCount);
            //是否接触过武汉人员
            CurrentConditionArray.contactWhPeople(dataByTime.get(i).getContactWhPeople(),contactWhPeople);
            //是否接触过发烧/咳嗽/感冒患者
            if(dataByTime.get(i).getContactOtherBadConditionPeople()==0){
                contactBadConditionPeople[1]++;
            }else {
                contactBadConditionPeople[0]++;
            }
            //是否去过或途径武汉
            CurrentConditionArray.contactWhPeople(dataByTime.get(i).getGoToWh(),goToWh);
            //近期是否有外出
            if(dataByTime.get(i).getGoOutNowdays()==0){
                goOut[1]++;
            }else {
                goOut[0]++;
            }
            //近期乘坐交通⼯具
            CurrentConditionArray.vehicle(dataByTime.get(i).getVehicle().split(","),vehicle);

        }
        jsonObject.put("currentBodyConditionCount",currentBodyConditionCount);
        jsonObject.put("contactWhPeople",contactWhPeople);
        jsonObject.put("contactBadConditionPeople",contactBadConditionPeople);
        jsonObject.put("goToWh",goToWh);
        jsonObject.put("goOut",goOut);
        jsonObject.put("vehicle",vehicle);
        jsonObject.put("userNum",size);
        jsonObject.put("usageScenarios",usageScenarios);
        return jsonObject;
    }

    @Override
    public void exportData(String groupId, String startdate, String enddate, HttpServletResponse response) {
        try {
            StringBuilder sd = new StringBuilder(startdate);
            startdate=sd.append(" 00:00:00").toString();
            StringBuilder ed = new StringBuilder(DateUtil.getNextDate(enddate));
            enddate = ed.append(" 00:00:00").toString();
            Map map = new HashMap();
            map.put("groupId",groupId);
            map.put("startdate", startdate);
            map.put("enddate", enddate);
            List<OfficeBuildingInfo> dataByTime = officeBuildingInfoMapper.getDataByTimeFull(map);
            int size = dataByTime.size();
            InputStream inputStream = getClass().getClassLoader().getResourceAsStream("files/officebuildingexport.xlsx");
            Workbook statisticsWorkBook =  new XSSFWorkbook(inputStream);
            Sheet peopleInfo = statisticsWorkBook.getSheet("详情");
            for (int i = 0; i < size; i++) {
                Row row = peopleInfo.createRow(i + 1);
                //姓名
                row.createCell(0).setCellValue(dataByTime.get(i).getUserName());
                //电话
                row.createCell(1).setCellValue(AesEncryptUtil.decrypt(dataByTime.get(i).getPhoneNumber()));
                //身份证号
                row.createCell(2).setCellValue(AesEncryptUtil.decrypt(dataByTime.get(i).getCardNumber()));
                //进入/离开小区
                row.createCell(3).setCellValue(dataByTime.get(i).getLeaveCommunity()==0?"离开":"进入");
                //进入/离开时间
                row.createCell(4).setCellValue(dataByTime.get(i).getEnterCommunityTime());
                //当前出入口
                row.createCell(5).setCellValue(dataByTime.get(i).getEntrance()==null?"":dataByTime.get(i).getEntrance());
                //进入楼宇号
                row.createCell(6).setCellValue(dataByTime.get(i).getBuildindNumber());
                //人员分类
                row.createCell(7).setCellValue(dataByTime.get(i).getUserType());
                //办公楼名称
                row.createCell(8).setCellValue(dataByTime.get(i).getCommunityName()==null?"":dataByTime.get(i).getCommunityName());
                //当前身体状况
                row.createCell(9).setCellValue(CurrentConditionArray.formatUserInfo(dataByTime.get(i).getCurrentBodyCondition()));
                //体温
                row.createCell(10).setCellValue(dataByTime.get(i).getHeatingDegree()==null?"":dataByTime.get(i).getHeatingDegree().toString());
                //过去两周有无不适
                row.createCell(11).setCellValue(dataByTime.get(i).getOtherDiscomfortLastweek()==0?"无":"有");
                //是否接触武汉人员
                row.createCell(12).setCellValue(dataByTime.get(i).getContactWhPeople()==0?"无":"有");
                //是否接触过发烧/咳嗽人员
                row.createCell(13).setCellValue(dataByTime.get(i).getContactOtherBadConditionPeople()==0?"无":"有");
                //是否去过或途径武汉
                row.createCell(14).setCellValue(dataByTime.get(i).getGoToWh()==0?"无":"有");
                //当天是否有外出
                row.createCell(15).setCellValue(dataByTime.get(i).getGoOutNowdays()==0?"无":"有");
                //乘坐交通工具
                row.createCell(16).setCellValue(CurrentConditionArray.formatVehicle(dataByTime.get(i).getVehicle()));
                //去过的城市
                row.createCell(17).setCellValue(dataByTime.get(i).getSomeplace()==null?"":dataByTime.get(i).getSomeplace());
                //车牌号
                row.createCell(18).setCellValue(dataByTime.get(i).getLicenseNumber()==null?"":dataByTime.get(i).getLicenseNumber());
            }
            FileUtils.downloadFile(statisticsWorkBook,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    @Override
    public String insertOfficeInfo(OfficeBuildingInfo officeBuildingInfo) {

        //获取当天时间
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
        Date date = new Date();
        String curTime = formatter.format(date);

        officeBuildingInfo.setAddTime(curTime);

        officeBuildingInfoMapper.insert(officeBuildingInfo);


        return null;
    }
}
